/**
 * 
 */
package com.shine.icis.dao.imp;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;

import com.shine.cotrun.sql.QueryParameters;
import com.shine.cotrun.sql.hibernate.CommQuery;
import com.shine.cotrun.web.comm.ListPage;
import com.shine.icis.dao.IRptA13DAO;
import com.shine.icis.dao.imp.BaseHibernateDAO;
import com.shine.icis.dto.RptA13;
import com.shine.icis.dto.RptA13.RptA13Id;
import com.shine.icis.hbean.RptA13Bak;

/**
 * @author zhangxr
 * 
 */
public class RptA13DAO extends BaseHibernateDAO implements IRptA13DAO {
	
	/**
	 * 分页查询A13列表
	 * @author zhangxr
	 * */
	@Override
	public ListPage<RptA13> findListPage(QueryParameters qp) {
		try {
			Query query = getSession().getNamedQuery("RptA13DAO.findListPage");
			Query countQuery = getSession().getNamedQuery(
					"RptA13DAO.findListPage_count");
			ListPage<Object> page = new CommQuery<Object>().queryListPage(qp,
					query, countQuery);
			ListPage<RptA13> accPage = new ListPage<RptA13>();
			accPage.setTotalCount(page.getTotalCount());
			accPage.setCurrentPageNo(page.getCurrentPageNo());
			accPage.setCurrentPageSize(page.getCurrentPageSize());
			if (page != null && page.getDataList() != null) {
				List<RptA13> datas = new ArrayList<RptA13>();
				for (Object o : page.getDataList()) {
					Object[] oArr = (Object[]) o;
					RptA13 rptA13 = new RptA13();
					rptA13.setId((String) oArr[0], (String) oArr[1],
							(String) oArr[3], (String) oArr[4],
							(String) oArr[5], (String) oArr[7],
							(String) oArr[6], (String) oArr[9], (String)oArr[12]);
					rptA13.setArapReason((String) oArr[2]);
					rptA13.setArapAmt((Double) oArr[8]);
					rptA13.setArapDate((String) oArr[10]);
					rptA13.setSummary((String) oArr[11]);
					datas.add(rptA13);
				}
				accPage.setDataList(datas);
			}
			return accPage;
		} catch (RuntimeException re) {
			re.printStackTrace();
			return new ListPage<RptA13>();
		}
	}
	
	@Override
	public ListPage<RptA13Bak> findBakListPage(QueryParameters qp) {
		if (qp == null) {
			throw new IllegalArgumentException("非法参数:查询参数对象为null");
		}
		try {
			StringBuffer sbQueryString = new StringBuffer();			
			sbQueryString.append("select b from RptA13Bak as b where 1=1");
			if(qp.getParameter("busiDate") != null){
				sbQueryString.append(" and b.busiDate=:busiDate");
			}
			if(qp.getParameter("opType") != null){
				sbQueryString.append(" and b.opType=:opType");
			}
			
			return new CommQuery<RptA13Bak>().queryListPage(qp, 
					qp.appendOrders(sbQueryString, "b" ), getSession());
		} catch(RuntimeException re) {
			re.printStackTrace();
			return new  ListPage<RptA13Bak>();
		}
	}
	
	/**
	 * 保存A13的信息
	 * @author zhangxr
	 * @param rptA13 A13实体信息
	 * */
	@Override
	public void save(RptA13 rptA13) {
		Query query = getSession().getNamedQuery("RptA13DAO.save");
		query.setString("companyCode",rptA13.getId().getCompanyCode());
		query.setString("tradeDate", rptA13.getId().getTradeDate());
		query.setString("arapType", rptA13.getId().getArapType());
		query.setString("oppPar", rptA13.getId().getOppPar());
		query.setString("mktCodeSipf", rptA13.getId().getMktCodeSipf());
		query.setString("arapBusiType", rptA13.getId().getArapBusiType());
		query.setString("curTypeSipf", rptA13.getId().getCurTypeSipf());
		query.setString("harapDate", rptA13.getId().getHarapDate());
		query.setString("busiDate", rptA13.getId().getBusiDate());
		
		query.setString("arapReason", rptA13.getArapReason());
		query.setDouble("arapAmt", rptA13.getArapAmt());
		if(rptA13.getArapDate() == null){
			query.setString("arapDate", null);
		} else {
			query.setString("arapDate", rptA13.getArapDate());
		}
		query.setString("summary", rptA13.getSummary());
		
		
		query.executeUpdate();
	}

	/**
	 * A13信息的修改
	 * 
	 * @author zhangxr
	 * @param oldId
	 *            原A13表的主键
	 * @param rptA13
	 *            A13实体
	 */
	@Override
	public void update(RptA13Id oldId, RptA13 rptA13) {
		Query query = getSession().getNamedQuery("RptA13DAO.update");
		query.setString("tradeDate", rptA13.getId().getTradeDate());
		query.setString("arapType", rptA13.getId().getArapType());
		query.setString("oppPar", rptA13.getId().getOppPar());
		query.setString("mktCodeSipf", rptA13.getId().getMktCodeSipf());
		query.setString("arapBusiType", rptA13.getId().getArapBusiType());
		query.setString("curTypeSipf", rptA13.getId().getCurTypeSipf());
		query.setString("harapDate", rptA13.getId().getHarapDate());

		query.setString("arapReason", rptA13.getArapReason());
		query.setDouble("arapAmt", rptA13.getArapAmt());
		if (rptA13.getArapDate() == null) {
			query.setString("arapDate", null);
		} else {
			query.setString("arapDate", rptA13.getArapDate());
		}
		query.setString("summary", rptA13.getSummary());
		query.setString("busiDate", rptA13.getId().getBusiDate());

		query.setString("oldCompanyCode", oldId.getCompanyCode());
		query.setString("oldTradeDate", oldId.getTradeDate());
		query.setString("oldArapType", oldId.getArapType());
		query.setString("oldOppPar", oldId.getOppPar());
		query.setString("oldMktCodeSipf", oldId.getMktCodeSipf());
		query.setString("oldArapBusiType", oldId.getArapBusiType());
		query.setString("oldCurTypeSipf", oldId.getCurTypeSipf());
		query.setString("oldHarapDate", oldId.getHarapDate());
		query.executeUpdate();
	}
	
	/**
	 * 根据主键，删除A13信息
	 * @author zhangxr
	 * @param id A13表主键
	 * */
	@Override
	public void delete(RptA13Id id) {
		Query query = getSession().getNamedQuery("RptA13DAO.delete");
		query.setString("companyCode", id.getCompanyCode());
		query.setString("tradeDate", id.getTradeDate());
		query.setString("arapType", id.getArapType());
		query.setString("oppPar", id.getOppPar());
		query.setString("mktCodeSipf", id.getMktCodeSipf());
		query.setString("arapBusiType", id.getArapBusiType());
		query.setString("curTypeSipf", id.getCurTypeSipf());
		query.setString("harapDate", id.getHarapDate());
		query.setString("busiDate", id.getBusiDate());
		query.executeUpdate();
		
	}
	
	/**
	 * 判断该主键是否已经存在，若存在，返回false，否则返回true
	 * @author zhangxr
	 * @param id A13表主键
	 * */
	@Override
	public boolean checkId(RptA13Id id) {
		Query query = getSession().getNamedQuery("RptA13DAO.checkId");
		query.setString("companyCode", id.getCompanyCode());
		query.setString("tradeDate", id.getTradeDate());
		query.setString("arapType", id.getArapType());
		query.setString("oppPar", id.getOppPar());
		query.setString("mktCodeSipf", id.getMktCodeSipf());
		query.setString("arapBusiType", id.getArapBusiType());
		query.setString("curTypeSipf", id.getCurTypeSipf());
		query.setString("harapDate", id.getHarapDate());
		query.setString("busiDate", id.getBusiDate());
		Long count = (Long)query.uniqueResult();
		if (count != null && count.longValue() > 0) {
			return false;
		}
		return true;
	}
	
	/**
	 * 新增A13的备份信息
	 * @author zhangxr
	 * @param rptA14Bak
	 * */
	@Override
	public void saveBak(RptA13Bak rptA14Bak) {
		this.save(rptA14Bak);
	}	
	
	/**
	 * 根据A13主键，获得A13相关信息
	 * @author zhangxr
	 * */
	@SuppressWarnings("unchecked")
	public RptA13 findRptA13ById(RptA13Id id) {
		RptA13 rptA13 = null;
		try {
			Query query = getSession().getNamedQuery("RptA13DAO.findRptA13ById");
			query.setString("companyCode", id.getCompanyCode());
			query.setString("tradeDate", id.getTradeDate());
			query.setString("arapType", id.getArapType());
			query.setString("oppPar", id.getOppPar());
			query.setString("mktCodeSipf", id.getMktCodeSipf());
			query.setString("arapBusiType", id.getArapBusiType());
			query.setString("curTypeSipf", id.getCurTypeSipf());
			query.setString("harapDate", id.getHarapDate());
			List<Object> list = query.list();
			if(list != null && list.size() > 0){
				rptA13 = new RptA13();
				Object[] oArr = (Object[])list.get(0);
				rptA13.setId((String) oArr[0], (String) oArr[1],
						(String) oArr[3], (String) oArr[4],
						(String) oArr[5], (String) oArr[7],
						(String) oArr[6], (String) oArr[9], (String) oArr[12]);
				rptA13.setArapReason((String) oArr[2]);
				rptA13.setArapAmt((Double) oArr[8]);
				rptA13.setArapDate((String) oArr[10]);
				rptA13.setSummary((String) oArr[11]);
			}
		} catch (RuntimeException re) {
			re.printStackTrace();
		}
		return rptA13;
	}
}
